package proxyServer;

public class ProxyHttpRequest
{
	private String host = new String();
	
	public String getHost()
	{
		return host;
	}
	
	private int port = 80;
	
	public int getPort()
	{
		return port;
	}
	
	private String m_strConnection = null;
	
	public String getConnection()
	{
		return m_strConnection;
	}
	
	public boolean isCloseConnection()
	{
		if (m_strConnection.compareTo("close") == 0) return true;
		return false;
	}
	
	public boolean parse(byte[] buffer)
	{
		String str = new String(buffer, 0, buffer.length);
		
		int start = str.indexOf("Host: ");
		if (start == -1)
		{
			return false;
		}
		start += 6;
		int end = str.indexOf('\n', start);
		host = str.substring(start, end - 1);
		if (host.contains(":"))
		{
			// Get the port of request
			start = host.lastIndexOf(":");
			try
			{
				port = Integer.parseInt(host.substring(start + 1));
				host = host.substring(0, start);
				
			}
			catch (Exception ex)
			{
				return false;
			}
		}
		start = str.indexOf("Connection: ");
		if (start == -1)
		{
			m_strConnection = null;
		} else
		{
			start += 12;
			end = str.indexOf('\n', start);
			m_strConnection = str.substring(start, end - 1);
		}
		return true;
		
	}
	
	/*
	 * Rebuilds the header in a string
	 * @returns The header in a string.
	 */
	
}
